package cn.com.backstage.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_order_return;
import cn.com.api.service.ApiT_order_detailService;
import cn.com.api.service.ApiT_order_returnService;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Utils;


public class T_order_returnBaseController {

	private static Logger logger = Logger.getLogger(T_order_returnBaseController.class);
	
	@Autowired
	private ApiT_order_returnService apiT_order_returnService;
	@Autowired
	private ApiT_order_detailService apiT_order_detailService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_order_return-list")
	public ModelAndView t_order_return_list(HttpServletRequest request, HttpServletResponse response, PageInfo page) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_order_return_list.jsp");
		Map<String, Object> cond = new HashMap<String, Object>();
		cond.put("orderNum", request.getParameter("orderNum"));
		cond.put("status", request.getParameter("status"));
		page.setShowCount(50000);
		List<ApiT_order_return> t_order_returns = apiT_order_returnService.findOrderReturnsListPage(cond,page);
		mv.addObject("t_order_returns", t_order_returns);
		return mv;
	}
	
	/**
	 * 退货详情
	 * 
	 * @author JW
	 * @param request
	 * @param response
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "detail_back")
	public ModelAndView detailBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/order-back-detail.jsp");
		mv.addObject("backOrderDetailList",
				apiT_order_detailService.getOrderDetailByOrderNum(request.getParameter("orderNum")));
		return mv;
	}

	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_order_return-add-pre")
	public ModelAndView t_order_return_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_order_return_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_order_return t_order_return = apiT_order_returnService.findT_order_returnById(Integer.valueOf(id));
			mv.addObject("t_order_return",t_order_return);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_order_return-add")
	public void t_order_return_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String user_id = request.getParameter("user_id");
		String order_id = request.getParameter("order_id");
		String create_date = request.getParameter("create_date");
		String goods_id = request.getParameter("goods_id");
		String product_id = request.getParameter("product_id");
		String return_money = request.getParameter("return_money");
		String return_name = request.getParameter("return_name");
		String return_reason = request.getParameter("return_reason");
		String return_phone = request.getParameter("return_phone");
		String reason = request.getParameter("reason");
		String duty = request.getParameter("duty");
		String money = request.getParameter("money");
		String check_time = request.getParameter("check_time");
		String img_1 = request.getParameter("img_1");
		String img_2 = request.getParameter("img_2");
		String img_3 = request.getParameter("img_3");
		String status = request.getParameter("status");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_order_return t_order_return = new ApiT_order_return();
            /*
		    t_order_return.setId(id);
		    t_order_return.setUser_id(user_id);
		    t_order_return.setOrder_id(order_id);
		    t_order_return.setCreate_date(create_date);
		    t_order_return.setGoods_id(goods_id);
		    t_order_return.setProduct_id(product_id);
		    t_order_return.setReturn_money(return_money);
		    t_order_return.setReturn_name(return_name);
		    t_order_return.setReturn_reason(return_reason);
		    t_order_return.setReturn_phone(return_phone);
		    t_order_return.setReason(reason);
		    t_order_return.setDuty(duty);
		    t_order_return.setMoney(money);
		    t_order_return.setCheck_time(check_time);
		    t_order_return.setImg_1(img_1);
		    t_order_return.setImg_2(img_2);
		    t_order_return.setImg_3(img_3);
		    t_order_return.setStatus(status);
		    t_order_return.setExt_1(ext_1);
		    t_order_return.setExt_2(ext_2);
		    t_order_return.setExt_3(ext_3);
		    t_order_return.setExt_4(ext_4);
		    t_order_return.setExt_5(ext_5);
	        */
			apiT_order_returnService.insertT_order_return(t_order_return);
		}
		//edit
		else{
			ApiT_order_return t_order_return = apiT_order_returnService.findT_order_returnById(Integer.valueOf(id));
	        /*
		    t_order_return.setId(id);
		    t_order_return.setUser_id(user_id);
		    t_order_return.setOrder_id(order_id);
		    t_order_return.setCreate_date(create_date);
		    t_order_return.setGoods_id(goods_id);
		    t_order_return.setProduct_id(product_id);
		    t_order_return.setReturn_money(return_money);
		    t_order_return.setReturn_name(return_name);
		    t_order_return.setReturn_reason(return_reason);
		    t_order_return.setReturn_phone(return_phone);
		    t_order_return.setReason(reason);
		    t_order_return.setDuty(duty);
		    t_order_return.setMoney(money);
		    t_order_return.setCheck_time(check_time);
		    t_order_return.setImg_1(img_1);
		    t_order_return.setImg_2(img_2);
		    t_order_return.setImg_3(img_3);
		    t_order_return.setStatus(status);
		    t_order_return.setExt_1(ext_1);
		    t_order_return.setExt_2(ext_2);
		    t_order_return.setExt_3(ext_3);
		    t_order_return.setExt_4(ext_4);
		    t_order_return.setExt_5(ext_5);
			*/
			apiT_order_returnService.updateT_order_return(t_order_return);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_order_return-delete")
	public void t_order_return_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_order_returnService.deleteT_order_return(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



